//
//  MasCampsDetailViewController.m
//  PartyAnimal
//
//  Created by Hilary Muludiang on 3/25/12.
//  Copyright 2012 Muludiang. All rights reserved.
//

#import "MasCampsDetailViewController.h"
#import "iBacchanal.h"
#import "AddMasCampSectionViewController.h"
#import "SectionPositionsViewController.h"
#import "PartyAnimalAppDelegate.h"
#import "UIImageView+AFNetworking.h"

@implementation MasCampsDetailViewController

@synthesize masSectionsTable;
@synthesize masSectionsData;
@synthesize sectionsTableData;
@synthesize sectionPositionsViewController;

#pragma mark -
#pragma mark Initialization

-(void)getMasSections{
	masSectionsData = [[NSMutableArray alloc] init];
	
	iBacchanal *bacchanal = [iBacchanal sharedUser];
	
	for (NSDictionary *mascamp in bacchanal.masBandsData){
		if([[mascamp objectForKey:@"mascamps_id"] isEqualToString:bacchanal.masBandID]){
			self.navigationItem.title = [mascamp objectForKey:@"mascampname"];
						
			NSURL *url = [NSURL URLWithString:[mascamp objectForKey:@"mascamplogo"] ];
			UIImageView *testView = [[UIImageView alloc] init];
			[testView setImageWithURL:url];

			self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:testView.image];
						
			if ([[mascamp objectForKey:@"mascamp_sections"]count] > 0){
				[masSectionsData addObject:[mascamp objectForKey:@"mascamp_sections"]];
			}
			else{
				masSectionsData = nil;
			}
		}
	}
	//NSLog(@"%@", masSectionsData);
}


-(IBAction)addMasSection:(UIBarButtonItem *)sender{
	AddMasCampSectionViewController *addMasCampSectionViewController = [[AddMasCampSectionViewController alloc] initWithNibName:@"AddMasCampSection" bundle:nil];
	UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:addMasCampSectionViewController];
	[addMasCampSectionViewController release];
	
	navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
	[self presentModalViewController:navController animated:YES];
	[navController release];
	
	
}
/*
 - (id)initWithStyle:(UITableViewStyle)style {
 // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
 self = [super initWithStyle:style];
 if (self) {
 // Custom initialization.
 }
 return self;
 }
 */


#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
	
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
	self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]  initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addMasSection:)] autorelease];
	
	masSectionsTable.backgroundColor = [UIColor clearColor];
	masSectionsTable.opaque = NO;
	masSectionsTable.backgroundView = nil;
}


/*
 - (void)viewWillAppear:(BOOL)animated {
 [super viewWillAppear:animated];
 }
 */

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
	[self getMasSections];
	[masSectionsTable reloadData];
	
}

/*
 - (void)viewWillDisappear:(BOOL)animated {
 [super viewWillDisappear:animated];
 }
 */
/*
 - (void)viewDidDisappear:(BOOL)animated {
 [super viewDidDisappear:animated];
 }
 */

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return YES; // (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
	if ([masSectionsData count] < 1){
		return 1;
	}
	else{
		return [masSectionsData count];
	}
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
	// NSLog(@"%i", [[masSectionsData objectAtIndex:section]count]);
	if ([masSectionsData count] < 1){
		return 1;
	}
	else{
		return [[masSectionsData objectAtIndex:section]count];
	}
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
	//NSLog(@"section_id %i", [[[[masSectionsData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"sections_id"] length]);
	if ([[[masSectionsData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"sections_id"] != nil ){
		
		[[cell textLabel] setText:[[[masSectionsData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"sectionname"]];
	}
	else{
		[[cell textLabel] setText:@"No sections found"];
	}
    return cell;
}


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
	//return ([NSString stringWithFormat:@"Sections (%i)", [[masSectionsData objectAtIndex:section]count]]);
	return @"Sections";
}

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */


/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source.
 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
 }   
 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
 }   
 }
 */


/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */


/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    /*
	 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
	 [self.navigationController pushViewController:detailViewController animated:YES];
	 [detailViewController release];
	 */
	
	if ([[[masSectionsData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"sections_id"] != nil ){
	SectionPositionsViewController *sectionPositionsView = [[SectionPositionsViewController alloc] initWithNibName:@"SectionPositions" bundle:nil];
	self.sectionPositionsViewController = sectionPositionsView;
	
	iBacchanal *bacchanal = [iBacchanal sharedUser];
	bacchanal.masBandSectionID = [[[masSectionsData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"sections_id"];

	[self.navigationController pushViewController:sectionPositionsView animated:YES];
	[sectionPositionsViewController release];
	
	//Reuse NavController
	PartyAnimalAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
	[delegate.partyAnimalNavigatorViewController pushViewController:sectionPositionsView animated:YES];
	}
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
	[masSectionsTable release];
	[masSectionsData release];
	[sectionsTableData release];
	[SectionPositionsViewController release];
    [super dealloc];
}


@end

